package com.taotao.rop.service;

import com.rop.annotation.HttpAction;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.response.BusinessServiceErrorResponse;
import com.rop.session.SimpleSession;
import com.taotao.admin.MyRopClient;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.RopAppSecret;
import com.taotao.rop.database.mapper.RopAppSecretMapper;
import com.taotao.rop.manager.RopSessionManager;
import com.taotao.rop.response.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class AppSecretService {
    @Autowired
    private RopAppSecretMapper ropAppSecretMapper;

    @ServiceMethod(method = "appSecret.insert", needInSession = NeedInSessionType.NO)
    public Object insertAppSecret(RopAppSecret request) {
        //检验appKey是否存在
        boolean isExist = true;
        List<RopAppSecret> ropAppSecretList = ropAppSecretMapper.getList(request);
        if (ropAppSecretList == null || ropAppSecretList.size() <= 0) {
            isExist = false;
        }
        if (isExist) {
            return new BusinessServiceErrorResponse(
                    request.getRopRequestContext().getMethod(), "APPKEY_EXISTED",
                    request.getRopRequestContext().getLocale(), request.getAppId()
            );
        }
        //插入
        Date now = new Date();
        request.setStatus(Constant.STATUS_ENABLE);
        request.setCreateDatetime(now);
        request.setLastModifyDatetime(now);
        Integer id = ropAppSecretMapper.insertOne(request);
        //判断插入是否成功
        if (id < 1) {
            return new BusinessServiceErrorResponse(
                    request.getRopRequestContext().getMethod(), "APPKEY_UNKNOWN",
                    request.getRopRequestContext().getLocale(), request.getAppId()
            );
        }

        BaseResponse<RopAppSecret> response = new BaseResponse<RopAppSecret>();
        response.setSuccess(true);
        return response;
    }

    @ServiceMethod(method = "appSecret.update", needInSession = NeedInSessionType.NO)
    public Object updateAppSecret(RopAppSecret request) {
        request.setLastModifyDatetime(new Date());
        Integer id = ropAppSecretMapper.updateOne(request);

        BaseResponse<RopAppSecret> response = new BaseResponse<RopAppSecret>();
        if (id > 0) {
            response.setSuccess(true);
        }
        return response;
    }

    @ServiceMethod(method = "appSecret.listAll", needInSession = NeedInSessionType.NO)
    public Object getAllAppSecret(RopAppSecret request) {
        List<RopAppSecret> ropAppSecretList = ropAppSecretMapper.getList(request);

        BaseResponse<List<RopAppSecret>> response = new BaseResponse<List<RopAppSecret>>();
        response.setSuccess(true);
        response.setMsg("" + ropAppSecretMapper.getCount(request));
        response.setData(ropAppSecretList);
        return response;
    }

    @ServiceMethod(method = "appSecret.list")
    public Object getAppSecret(RopAppSecret request) {
        String appId = ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_APP_ID).toString();
        if (!appId.equalsIgnoreCase(MyRopClient.APP_ID)) {
            request.setAppId(appId);
        }
        List<RopAppSecret> ropAppSecretList = ropAppSecretMapper.getList(request);

        BaseResponse<List<RopAppSecret>> response = new BaseResponse<List<RopAppSecret>>();
        response.setSuccess(true);
        response.setMsg("" + ropAppSecretMapper.getCount(request));
        response.setData(ropAppSecretList);
        return response;
    }

    @ServiceMethod(method = "appSecret.listByPage", needInSession = NeedInSessionType.NO)
    public Object getAppSecretByPage(RopAppSecret request) {
        String appId = ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_APP_ID).toString();
        if (!appId.equalsIgnoreCase(MyRopClient.APP_ID)) {
            request.setAppId(appId);
        }
        List<RopAppSecret> ropAppSecretList = ropAppSecretMapper.getListByPage(request);

        BaseResponse<List<RopAppSecret>> response = new BaseResponse<List<RopAppSecret>>();
        response.setSuccess(true);
        response.setMsg("" + ropAppSecretMapper.getCount(request));
        response.setData(ropAppSecretList);
        return response;
    }
}
